/***************************************************************************
 *   Copyright (C) 2008 by Chris Vickery   *
 *   chrisinajar@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "socketthread.h"

#include <QSslSocket>
#include <QDebug>
#include <QDateTime>
#include <QTimer>

#include <qsleeper.h>

SocketThread::SocketThread(int s, QObject *parent) : QThread(parent) {
	sdesc=s;
}

void SocketThread::run() {
	socket = new QSslSocket();
	connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socketError(QAbstractSocket::SocketError)));
	connect(socket,SIGNAL(sslErrors(QList<QSslError>)),this,SLOT(sslErrors(QList<QSslError>)));
	socket->setSocketDescriptor(sdesc);
	if(socket->isValid() && socket->state()==QAbstractSocket::ConnectedState) {
		qDebug() << "Socket was initialized properly.";
	} else {
		qDebug() << "Socket was not initialized properly" << socket->errorString();
	}
}

bool SocketThread::waitForCanReadLine(int t) {
	int start=QDateTime::currentDateTime().toTime_t();
	while((QDateTime::currentDateTime().toTime_t()-start)<t) {
		socket->waitForReadyRead(t);
		if(socket->canReadLine())
			return true;
		QSleeper::msleep(10);
		if(hasError());
	}
	if(socket->canReadLine())
		return true;
	return false;
}

QString SocketThread::getFirstLine() {
	
}

QString SocketThread::readLine() {
	waitForCanReadLine();
	QString line=socket->readLine();
	if(line.isNull()) {
		qDebug() << "Error, empty line";
	}
}

bool SocketThread::writeLine(QString line) {
	while(!socket->isEncrypted()) {
		QSleeper::msleep(10);
	}
	int w = socket->write(line.toAscii().data());
	qDebug() << "Wrote" << w << "bytes to the network socket";
	if(w<1)
		return false;
	return true;
}

bool SocketThread::hasError() {
	return false;
}

/* SLOTS */
void SocketThread::readyRead() {
	bool newline=false;
	while(socket->canReadLine()) {
		lines << readLine();
		newline=true;
	}
	if(newline)
		emit newLine();
}

void SocketThread::socketError(QAbstractSocket::SocketError error) {
	qDebug() << "Received socket related error:" << socket->errorString();
}

void SocketThread::sslErrors(QList<QSslError> errors) {
	foreach(QSslError error,errors) {
		qDebug() << "Received SSL related error:" << error.errorString();
		socket->ignoreSslErrors();
	}
}

SocketThread::~SocketThread() {
}


